﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleFeeDerRepo
    {
        public const string m_msg_head = "Title FeeDer";

        public IEnumerable<TitleFeeDerModels> GetTitleFeeDer()
        {
            List<TitleFeeDerModels> TitleFeeDers = new List<TitleFeeDerModels>();
            string SQLCommand = @"select * from TBTitleFeeDer WHERE Status <> 'Delete' Order by TitleFeeDerId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleFeeDerModels TitleFeeDer = new TitleFeeDerModels();
                        TitleFeeDer.TitleFeeDerId = (int)reader["TitleFeeDerId"];
                        TitleFeeDer.TitleFeeDerName = (string)reader["TitleFeeDerName"];
                        TitleFeeDer.Status = (string)reader["Status"];

                        TitleFeeDers.Add(TitleFeeDer);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleFeeDers.ToList();
        }

        public TitleFeeDerModels GetTitleFeeDerById(int id)
        {
            TitleFeeDerModels TitleFeeDer = new TitleFeeDerModels();
            string SQLCommand = @"select * from TBTitleFeeDer where TitleFeeDerId=@TitleFeeDerId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleFeeDerId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleFeeDer.TitleFeeDerId = (int)reader["TitleFeeDerId"];
                        TitleFeeDer.TitleFeeDerName = (string)reader["TitleFeeDerName"];
                        TitleFeeDer.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleFeeDer;
        }

        public bool Create(TitleFeeDerModels TitleFeeDer)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleFeeDer
                                    values (@TitleFeeDerName,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleResultName", TitleFeeDer.TitleFeeDerName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleFeeDerModels TitleFeeDer)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleFeeDer 
                                    Set TitleFeeDerName=@TitleFeeDerName
                                    where TitleFeeDerId=@TitleFeeDerId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleFeeDerId", TitleFeeDer.TitleFeeDerId);
                    db.AddParameter("@TitleFeeDerName", TitleFeeDer.TitleFeeDerName);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleFeeDerModels TitleFeeDer)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleFeeDer
                                    Set Status=@Status
                                    where TitleFeeDerId=@TitleFeeDerId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleFeeDerId", TitleFeeDer.TitleFeeDerId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}